package com.sgb.kjwl.viewmodel.home;

import android.text.TextUtils;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.ZulinMenuEntity;
import com.swgk.core.view.entity.EnterpriseEditEntity;
import com.sgb.kjwl.model.entity.lease.AskLeaseItemEntity;
import com.sgb.kjwl.model.entity.lease.LeaseMapEntity;
import com.sgb.kjwl.model.entity.request.LeaseHallMapFilterJson;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.fragment.home.LeaseFragment3;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LeaseFragment3Model extends BaseViewModel {
    public HomeRepertory repertory;
    private LeaseFragment3 fragment;

    public int page = 0;
    public int pageSize = 10;

    public LeaseFragment3Model(LeaseFragment3 fragment, HomeRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }


    /**
     * 菜单分类
     */
    public void getMenu(int type) {
//        String json = MkvUtils.get().getString(MkvUtils.CacheModular, CacheMarker.LeaseMenu, "");
//        if (!TextUtils.isEmpty(json)) {
//            List<List<ZulinMenuEntity>> data1 = new Gson().fromJson(json, new TypeToken<List<ZulinMenuEntity>>() {
//            }.getType());
//            fragment.showMenuView(data1);
//            Log.i("xxx","dataJson "+"走缓存");
//        }
        APIManager.getInstance().getHomeLeaseAPI().getLeaseHomeMenu(type+1).enqueue(new ICallBack<BaseEntity<List<List<ZulinMenuEntity>>>>(true) {
            @Override
            public void onSuccess(BaseEntity<List<List<ZulinMenuEntity>>> data) {
                        List<List<ZulinMenuEntity>> list = data.getData();
//                        String dataJson = DatalUtils.getInstance().getJsonString(data);
                        //     Log.i("xxx","dataJson "+dataJson);
//                        String mHashNet = DatalUtils.strToHashKey(dataJson);
//                        String hashData = DatalUtils.getHashData(MkvUtils.CacheModular, CacheMarker.LeaseMenu);
//                        boolean isEqual = DatalUtils.getInstance().IsCahceEqual(data, CacheMarker.LeaseMenu);
//                        if (!isEqual) {
                        fragment.showMenuView(list);
//                          MkvUtils.get().putString(MkvUtils.CacheModular, CacheMarker.LeaseMenu, dataJson);
//                        }
            }

        });
    }

    //获取筛选列表
    public void getLeaseMapSearch(LeaseHallMapFilterJson filterJson) {
        filterJson.setType("");
        filterJson.setCurrent(1);
        filterJson.setLimit(50);
//        if(TextUtils.isEmpty(filterJson.getLatitude())){
        if (repertory.getIsLocal()) {
            //经度
            filterJson.setLatitude("".equals(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LATITUDE_ID, ""))
                    ? repertory.getLatitude() : SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LATITUDE_ID, ""));
            //纬度
            filterJson.setLongitude("".equals(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LONGITUDE_ID, ""))
                    ? repertory.getLongititude() : SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LONGITUDE_ID, ""));
        } else {
            //经度
            filterJson.setLongitude(repertory.getLongititude());
            //纬度
            filterJson.setLatitude(repertory.getLatitude());
        }

//        }

        APIManager.getInstance().getLeaseMapAPI().getLeaseMapSearch(filterJson)
                .enqueue(new Callback<BaseEntity<ListResponse<List<LeaseMapEntity>>>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<ListResponse<List<LeaseMapEntity>>>> call, Response<BaseEntity<ListResponse<List<LeaseMapEntity>>>> response) {
                        if (response.body() == null || response.body().getState() == null) {
                            return;
                        }
                        if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                            fragment.showMapMark(response.body().getData().getList());
                        }

                    }

                    @Override
                    public void onFailure(Call<BaseEntity<ListResponse<List<LeaseMapEntity>>>> call, Throwable t) {
                        t.printStackTrace();
                    }
                });
    }

    //获取类型分类数据
    public void getTypeModel() {
        DialogHelper.showProgressDialog(fragment.getActivity(), "加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getMachineAPI().getAllTypeList().enqueue(new Callback<BaseEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>>>> call, Response<BaseEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (null != response.body().getData()) {
                        fragment.showTypeView(response.body().getData());
                    } else {
                        MToast.showToast(fragment.getActivity(), "暂无数据");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //求租类型
    public List<DictEntity3> getType() {
        List<DictEntity3> typeList = new ArrayList<>();
        typeList.add(new DictEntity3("0", "机械求租"));
        typeList.add(new DictEntity3("1", "设备求租"));
        typeList.add(new DictEntity3("2", "车辆求租"));
        typeList.add(new DictEntity3("3", "房产求租"));
        typeList.add(new DictEntity3("4", "土地求租"));
        typeList.add(new DictEntity3("5", "工厂求租"));
        typeList.add(new DictEntity3("6", "农机求租"));
        return typeList;
    }

    //获取汽车类型分类信息
    public void getModelSelect() {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getMachineAPI().getModelSelect().enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.showCarTypeView(response.body().getData());
                } else {
                    MToast.showToast(fragment.getActivity(), response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    int i = 0;
    /**
     * 根据用户身份信息获取展示的企业标题
     */
    public void getCompanyTitleList(String areaCode) {
        i++;
        areaCode = repertory.getAreaCode();
        SharedPreferenceUtil.getInstance().saveString(SharedPreferenceUtil.AREA_CODE, repertory.getAreaCode());
        int code = TextUtils.isEmpty(areaCode)?615000:Integer.valueOf(areaCode);
        APIManager.getInstance().getBigHomeAPI().getEnterpriseRecListNew(code).enqueue(new ICallBack<BaseEntity<List<EnterpriseEditEntity>>>(true, true) {
            @Override
            public void onSuccess(BaseEntity<List<EnterpriseEditEntity>> data) {
                i=0;
                fragment.initCompanyTitle(data.getData());
            }

            @Override
            public void onFailureWithEntity(BaseEntity entity) {
                super.onFailureSwitchErrCode(entity);
                if (entity != null && i<2){
                    if ("53999".equals(entity.getErrCode())){
                        getCompanyTitleList("");
                    }
                }
            }
        });
    }
}
